package com.ccl.iot.views;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ccl.iot.R;
import com.ccl.iot.device.DeviceIconProvider;
import com.ccl.iot.device.IOTProbe;

/**
 * 创建者     王浩
 * 创建时间   2017/4/20 13:47
 * 描述	      ${TODO}
 */

public class ProbeView extends RelativeLayout {
    public static int STATE_NORMAL = 0;
    public static int STATE_DELETE = 1;

    private ImageView mIvProbe, mIvProbeDelete;
    private TextView mTvCount;
    private MarqueeLabel mMqlName;
    private int mIndex;

    public ProbeView(Context context) {
        this(context, null);
    }

    public ProbeView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ProbeView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        View.inflate(getContext(), R.layout.view_probe, this);
        mIvProbe = (ImageView) findViewById(R.id.iv_probe);
        mIvProbeDelete = (ImageView) findViewById(R.id.iv_delete_probe);
        mTvCount = (TextView) findViewById(R.id.tv_probe_new_info);
        mMqlName = (MarqueeLabel) findViewById(R.id.mql_probe_name);
    }

    public ImageView getIvProbeView() {
        return mIvProbe;
    }

    public void initData(IOTProbe dev, int index) {
        if (dev != null) {
            mIndex = index;
            mMqlName.setText(dev.getName(index));

            int newTriggerCount = dev.getProbes().get(index).getNewTriggerCount();
            String count;
            if(newTriggerCount >= 100){
                count = "99+";
            }else{
                count = newTriggerCount+"";
            }
            mTvCount.setText(count);

            mIvProbe.setImageResource(DeviceIconProvider.getProbeSelectorId(dev.getIcon(index)));
            mIvProbe.setTag(index);
            mIvProbeDelete.setTag(index);
        }
    }

    public int getIndex() {
        return mIndex;
    }

    public void setOnProbeClickListener(OnClickListener l, OnLongClickListener ll) {
        if (l != null) {
            mIvProbe.setOnClickListener(l);
            mIvProbeDelete.setOnClickListener(l);
        }

        if(ll != null){
            mIvProbe.setOnLongClickListener(ll);
        }
    }

    public void updateView(int state) {
        if (state == STATE_NORMAL) {
            mIvProbeDelete.setVisibility(View.GONE);
        } else if (state == STATE_DELETE) {
            mIvProbeDelete.setVisibility(View.VISIBLE);
        }
    }

}
